package maven.jyhs.com.controller;

import com.alibaba.fastjson2.JSON;
import maven.jyhs.com.entity.Menu;
import maven.jyhs.com.entity.Student;
import maven.jyhs.com.servce.MenuSevrce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/menu", produces = "application/json;charset=UTF-8")
public class MenuController {
    //    @RequestMapping(value = {"/","/index",})
    @Autowired
    private MenuSevrce menuSevrce;

    @GetMapping("/index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        modelAndView.addObject("title", "菜单管理");
        return modelAndView;
    }


    @RequestMapping("/del")
    public String del(@RequestParam(value = "name1", required = false, defaultValue = "123") String name,
                      @RequestParam(value = "age", defaultValue = "13") String age) {
        System.out.println("name:" + name);
        System.out.println("age:" + age);
        throw new RuntimeException("del error");
//        System.out.println("del");
//        return "del";
    }


    @RequestMapping("/del1")
    public String del1(Menu menu) {
        System.out.println("name:" + menu);
        System.out.println("del1");
        return JSON.toJSONString(menu);
    }

    @GetMapping(value = "selectList")
    public String selectList() {
        System.out.println("selectList");
        List<Menu> menus = menuSevrce.getMenuList();
        Menu menu = new Menu();
        menu.setId(0);
        setChild(menu, menus);

        Map map = new HashMap();
        Map homeInfoMap = new HashMap();
        homeInfoMap.put("title", "首页");
        homeInfoMap.put("href", "page/welcome-1.html?t=1");
        Map logoInfoMap = new HashMap();
        logoInfoMap.put("title", "logo");
        logoInfoMap.put("href", "page/welcome-2.html?t=1");
        logoInfoMap.put("image", "images/logo.png");

        map.put("homeInfo", homeInfoMap);
        map.put("logoInfo", logoInfoMap);
        map.put("menuInfo", menu.getChild());
        return JSON.toJSONString(map);


    }

    @GetMapping(value = "select")
    public String select() {
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", "19");
        map.put("data", menuSevrce.getMenuList());
        return JSON.toJSONString(map);
    }

//    @PostMapping("/add")
//    public String add1(@RequestBody Student menu) {
//        System.out.println(menu);
//        return menu.toString();
//    }
//
//    @PostMapping("/addMenu")
//    @ResponseBody
//    public String addMenu(@RequestBody Menu menu) {
//        // 假设你的 menuService.addMenu 方法返回添加后的 Menu 对象
//        boolean addedMenu = menuSevrce.addMenu(menu);
//        return JSON.toJSONString(addedMenu);
//
//    }

    public void setChild(Menu menu, List<Menu> list) {
        List<Menu> childList = new ArrayList<>();
        for (Menu menu1 : list) {
            if (menu1.getPid() == menu.getId()) {
                setChild(menu1, list);
                childList.add(menu1);
            }
        }
        menu.setChild(childList);
    }


}
